<template>
  <div>

    <!-- 下拉框 -->
    <el-form :inline="true" ref="form" :model="records" label-width="45px" class="demo-form-inline" style="margin-top: 25px;margin-left: 25px">
      <el-form-item style="margin-left: 15px">   <!--    label="全部仓库"    -->
        <el-select v-model="keyword.recordsStats"  placeholder="选择仓库" @change="search">
          <el-option v-for="roleType in List3" :label="roleType" :value="roleType" />
        </el-select>
      </el-form-item>

      <el-form-item style="margin-left: 15px" v-if="show==true">              <!---->
        <el-select v-model="keyword.recordsType"  placeholder="选择分类" @change="search" >
          <el-option v-for="roleType in List3" :label="roleType" :value="roleType" />
        </el-select>
      </el-form-item>
    </el-form>

    <!--增删改查-->
    <el-form :inline="true" :model="keyword" ref="keyword" class="demo-form-inline" style="margin-left: 40px;">
      <template slot-scope="scope">
        <el-button type="success" size="small" plain icon="el-icon-plus" @click="openDialog">新增</el-button>
        <el-button type="warning" size="small" plain icon="el-icon-edit" @click="updataRecords">修改</el-button>
        <el-button type="danger" size="small" plain icon="el-icon-delete" @click="delBatch">删除</el-button>
        <el-button style="background-color: #e8d5ec" size="small" plain icon="el-icon-download">导入</el-button>
        <el-button type="primary" size="small" plain icon="el-icon-upload">导出</el-button>
      </template>
    </el-form>

    <!-- 表格数据区域 -->
    <el-table :row-class-name="tableRowClassName" :data="recordsList" @selection-change="handleSelectionChange"
      style="width: 100%;margin-bottom: 20px;"
      row-key="recordsId"
      max-height="525">  <!--   流体高度   -->
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="recordsSku"
        sortable
        label="品名/SKU"
        width="150">
      </el-table-column>
      <el-table-column
        prop="recordsStats"
        label="仓库"
        width="100">
      </el-table-column>
      <el-table-column
        prop="recordsType"
        sortable
        width="130"
        label="分类"></el-table-column>
      <el-table-column
        prop="recordsBrand"
        width="110"
        label="品牌"></el-table-column>
      <el-table-column
        prop="listUnit"
        width="100"
        label="单位">
      </el-table-column>
      <el-table-column
        prop="listKeyong"
        width="100"
        label="可用量">
      </el-table-column>
      <el-table-column
        prop="listFahuo"
        width="100"
        label="待发货量">
      </el-table-column>
      <el-table-column
        prop="listCipin"
        width="100"
        label="次品量">
      </el-table-column>
      <el-table-column
        prop="recordsDaohuo"
        width="100"
        label="待到货量">
      </el-table-column>
      <el-table-column
        prop="recordsRelZong"
        width="100"
        label="实际总量"/>
        <el-table-column
        prop="listHuozhi"
        width="100"
        label="货值"/>
      <el-table-column
        prop="recordsMoney"
        width="100"
        label="费用"/>
      <el-table-column
        prop="listChengben"
        width="100"
        label="库存成本">
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            style="color: #c9880c"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            style="color: red"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/修改对话框 -->
    <el-dialog title="添加库存" :visible.sync="dialogFormVisible" width="44%">
      <el-form :model="records" :rules="stashRecodesRules" ref="stashRecodesRulesForm">
        <el-form-item label="品名/SKU" prop="recordsSku">
          <el-input v-model="records.recordsSku" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="仓库" prop="recordsStats">
          <el-input v-model="records.recordsStats" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="分类" prop="recordsType">
          <el-input v-model="records.recordsType" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="品牌" prop="recordsBrand">
          <el-input v-model="records.recordsBrand" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <!--   点确定触发函数  进行校验 -->
        <el-button type="primary" @click="addStashRecords('stashRecodesRulesForm')">确 定</el-button>
      </div>
    </el-dialog>

    <!--完整功能分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[1, 5, 7, 10,15,20]"
      :page-size="15"
      layout="->, total, sizes, prev, pager, next, jumper"
      :total="15">
    </el-pagination>

  </div>
</template>

<script>
import { getList, delRecords, addRecords, delBatch, editRecords, findList } from '@/api/records'
import { title } from '@/settings'
export default {
  name: 'Records',
  data() {
    return {
      keyword: { // 添加查询关键词对象
        recordsId: '',
        recordsStats: '',
        recordsType: ''
      },
      List3: [],
      ids: [],
      currentPage: 1,
      recordsList: [],
      pageNum: 1,
      pageSize: 15,
      total: 0,
      loading: true,
      title: '',
      dialogFormVisible: false,
      records: {
        recordsSku: null,
        recordsStats: null,
        recordsType: null,
        recordsBrand: null,
        listUnit: null,
        listKeyong: null,
        listFahuo: null,
        listCipin: null
      },
      stashRecodesRules: {
        recordsSku: [
          { required: true, message: '请输入品名/SKU', trigger: 'blur' }
        ],
        recordsStats: [
          { required: true, message: '请输入仓库', trigger: 'blur' }
        ],
        recordsType: [
          { required: true, message: '请输入分类', trigger: 'blur' }
        ],
        recordsBrand: [
          { required: true, message: '请输入品牌', trigger: 'blur' }
        ]
      },
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      }
    }
  },
  methods: {
    tableRowClassName({row, rowIndex}) {
      if (rowIndex === 1) {
        return 'success-row';
      } else if (rowIndex === 3) {
        return 'warning-row';
      } else if (rowIndex === 5) {
        return 'danger-row';
      } else if (rowIndex === 7) {
        return 'primary-row';
      } else if (rowIndex === 9) {
        return 'success-row';
      }else if (rowIndex === 11) {
        return 'warning-row';
      }else if (rowIndex === 13) {
        return 'danger-row';
      } else if (rowIndex === 15) {
        return 'primary-row';
      }
      return '';
    },
    show() {

    },
    updataRecords() {

    },
    fetchData(){ // 获取数据
      let params = {
        pageNum:this.pageNum,
        pageSize:this.pageSize,
        recordsId:this.keyword.recordsId,
        recordsStats:this.keyword.recordsStats,
        recordsType:this.keyword.recordsType
      }
      getList(params).then(res => {
        let pageInfo = res.data;
        this.loading = false;
        this.recordsList = pageInfo.list;
        this.total = pageInfo.total
      })
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData(); // 重新获取数据
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.fetchData(); // 重新获取数据
    },
    // 搜索
    search() {
      this.fetchData()
    },
    // 重置
    resetSearchForm(formName) {
      this.$refs[formName].resetFields();
      this.fetchData()
    },
    // 添加
    openDialog() {
      this.records = {}
      this.dialogFormVisible = true;
      this.title = "添加"
    },
    // 打开更新
    handleUpdate(row) {
      this.dialogFormVisible = true;
      this.records = row
      this.title = "更改"
    },
    // 确认添加数据
    addStashRecords(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.title === "更改") {
            editRecords(this.records).then(res => {
              alert('更改成功!')
              this.dialogFormVisible = false
              this.fetchData()
            })
          } else if (this.title === "添加") {
            addRecords(this.records).then(res => {
              alert('添加成功!')
              this.fetchData()
            })
          }
        } else {
          return false
        }
        this.dialogFormVisible = false
      })
    },
      // 校验固定写法
      // this.$refs[formName].validate((valid) => {
      //   if (valid) {
      //     // 在此处执行添加...
      //     // console.log(this.records)
      //     addRecords(this.records).then(res=>{
      //       this.fetchData();
      //       this.$message({
      //         type: 'success',
      //         message: '添加成功!',
      //       })
      //       this.fetchData();
      //     })
      //   } else {
      //     console.log('error submit!!');
      //       return false;
      //   }
      //   this.dialogFormVisible = false;
      // });
    // ID删除
    handleDelete(row){
      this.$confirm('是否确认删除品名为'+row.recordsSku+'的数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          id:row.recordsId
        }
        delRecords(params).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.fetchData();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = [];
      for (let i = 0; i < selection.length; i++) {
        this.ids.push(selection[i].recordsId)
      }
    },
    // 批量删除
    delBatch() {
      this.$confirm('是否确认删除['+this.ids+']的数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // let params = {
        // }
        delBatch({ids:this.ids.join(",")}).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.fetchData();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    find3() {
      findList().then(res => {
        this.List3 = res.data
        this.fetchData()
      })
    }
  },
  created() {
    this.fetchData()
    this.find3()
  },
}
</script>

<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
.el-table .danger-row {
  background: #fef0f0;
}
.el-table .primary-row {
  background: #ecf5ff;
}
</style>
